package com.msb.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.msb.sentinel.handler.SentinelBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/****************************************************
 * 使用 @SentinelResource 的属性blockHandler 实现自定义限流逻辑处理
 *
 * @author lbf
 * @date 2025/6/25 16:32
 ****************************************************/
@RestController
public class SentinelResourceController {

    @GetMapping("/r")
    /**
     * 通过属性 blockHandler 指定触发限流后的处理方法
     */
    @SentinelResource(value = "resource",blockHandler = "FlowlimitHandler")
    public String resource(){

        return "+++++++++ SentinelResource 限流处理++++++++++";
    }

    @GetMapping("/r2")
    /**
     * 通过属性 blockHandler 指定触发限流后的处理方法
     */
    @SentinelResource(value = "byHnadlerClass",blockHandlerClass = SentinelBlockHandler.class,blockHandler = "blockHandler")
    public String resourceByClass(){

        return "+++++++++ SentinelBlockHandler 限流处理++++++++++";
    }

    /**
     * 该方法参数列表：源@SentinelResource所标注方法的参数列表、BlockException
     *
     * @param ex
     * @return
     */
    public String FlowlimitHandler(BlockException ex){

        return "FlowlimitHandler";
    }
}
